﻿<?php
// no direct access
defined( '_JEXEC' ) or die( 'Acces interdit' );

jimport( 'joomla.application.component.model' );

class TournoiModelCreation extends JModel {

	function __construct() {
		parent::__construct();

		// On ne crée le tournoi en base que si sa description est valorisée
		if (JRequest::getVar('description') != '')
			$this->insertTournoi();
	}

/**
	 * Methode pour construire la requete SQL d'insertion de tournoi
	 * @return string de la requete SQL
	 */
	function _buildQueryTournoi() {
		$query = null;
		
		// informations generales
		$titre = JRequest::getVar('titre');
		$description = JRequest::getVar('description');
		$lieu = JRequest::getVar('lieu');
		
		list($jour,$mois,$annee) = explode('/', JRequest::getVar('datedebut'));
		$datedebut = $annee.'-'.$mois.'-'.$jour;
		$heuredebut = JRequest::getVar('heuredebut');
		
		// information tournois
		$idVariantes = JRequest::getVar('variantes');
		$buyIn = JRequest::getVar('buyIn');
		$tapisDepart = JRequest::getVar('tapisDepart');
		
		$recave = JRequest::getVar('recave');
		if (JRequest::getVar('montantRecave') != '')
			$montantRecave = JRequest::getVar('montantRecave');
		else
			$montantRecave = 0;
		if (JRequest::getVar('jetonRecave') != '')
			$jetonRecave = JRequest::getVar('jetonRecave');
		else
			$jetonRecave = 0;
		
		$addOn = JRequest::getVar('addOn');
		if (JRequest::getVar('montantAddon') != '')
			$montantAddon = JRequest::getVar('montantAddon');
		else
			$montantAddon = 0;
		if (JRequest::getVar('jetonAddon') != '')
			$jetonAddon = JRequest::getVar('jetonAddon');
		else
			$jetonAddon = 0;
			
		$user = & JFactory::getUser();
		$createur = $user->get('id');
		
		$query = "INSERT INTO #__tournois 
					(`idVariantes`, `buyin`, `chips`, `prizepool`, `statut`, `recave`, `recave_montant`, `recave_chips`, `addon`, `addon_montant`, `addon_chips`, `payes`, `dateDebut`, `heure`, `lieu`, `titre`, `description`, `createur`, `idStructures`) 
					VALUES ($idVariantes, $buyIn, $tapisDepart, 0, '', $recave, $montantRecave, $jetonRecave, $addOn, $montantAddon, $jetonAddon, 0, '$datedebut', '$heuredebut', '$lieu', '$titre', '$description', $createur, 1);";
		
		if (JDEBUG) echo "<br />DEBUG : query SQL=".$query."<br><br>";

		return $query;
	}
	
	/**
	 * Methode pour construire la requete SQL d'insertion des participants au tournois
	 * @return string de la requete SQL
	 */
	function _buildQueryParticipants($tournoiId, $joueurId)
	{
		$query = null;
		$query = "INSERT INTO #__joueurs (`idUsers`, `idTournois`, `place`, `gain`, `recave`, `addon`, `statut`) VALUES ($joueurId, $tournoiId,0,0,0,0,'Non inscrit');";
		return $query;
	}
	
  function _getVarianteQuery()
  {
    $db = JFactory::getDBO();
    $id = @$options['id'];
    $query = 'SELECT * FROM #__variantes';
    return $query;
  }

  function getVariantes()
  { 
    $query = $this->_getVarianteQuery();
    $result = $this->_getList($query);
    return @$result;
  }	
  
  function _getUsersQuery()
  {
    $db = JFactory::getDBO();
    $id = @$options['id'];
    $query = 'SELECT * FROM #__users';
    return $query;
  }

  function getUsers()
  { 
    $query = $this->_getUsersQuery();
    $result = $this->_getList($query);
    return @$result;
  }  

	function insertTournoi()
	{
		$db = JFactory::getDBO();
		$db->setQuery($this->_buildQueryTournoi());
		$result = $db->query();
		
		if ($result != 1)
			echo "ERREUR ! Tournoi non enregistré en base !";
		else
		{
			$tournoi_id = $db->insertid(); // Récupère l'id de l'enregistrement stocké en base
			
			if(($_POST['participants']) && !empty($_POST['participants']))
			{ 
				foreach($_POST["participants"] as $value)
				{
					$db->setQuery($this->_buildQueryParticipants($tournoi_id, $value));
					$result = $db->query();
					if ($result != 1)
						echo "ERREUR ! Participants non associés au tournoi en base !";
				} 		
			}
		}
			
	} 
}
?>
